package com.example.mq.jms;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class QueueConsumer {

    //默认用户名
    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
    //默认密码
    private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
    //默认连接地址
    private static final String BROKER_URL = ActiveMQConnection.DEFAULT_BROKER_URL;

    public static void main(String[] args) {
        //创建连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKER_URL);
        try {
            //创建连接
            Connection connection = connectionFactory.createConnection();
            //启动连接
            connection.start();
            //创建会话
            final Session session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE);
            //创建队列，需要指定队列名称，消息生产者和消费者将根据它来发送、接收对应的消息
            Queue myTestQueue = session.createQueue("activemq-queue-test1");
            //消息消费者
            MessageConsumer consumer = session.createConsumer(myTestQueue);
            //消费者实现监听接口消费消息
            consumer.setMessageListener(new MessageListener() {
                @Override
                public void onMessage(Message message) {
                    try {
                        TextMessage textMessage = (TextMessage) message;
                        System.out.println(textMessage.getText());
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                    try {
                        session.commit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
            //让主线程休眠100秒，使消息消费者对象能够存活一段时间，从而能监听到消息
            Thread.sleep(100*1000);
            //关闭资源
            session.close();
            connection.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
